//
//  BanBu_SmileScrolView.m
// Halfarea
//
//  Created by Wang Xj on 14-3-19.
//
//

#import "BanBu_SmileScrolView.h"
#import "AppCommunicationManager.h"


#define DeleteTag 999

@implementation BanBu_SmileScrolView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _type=999999999;
        
        UIImageView *bkView = [[UIImageView alloc] initWithFrame:self.bounds];//-40
        bkView.image = [UIImage imageNamed:@"chatbg.png"];
        [self addSubview:bkView];
        [bkView release];
        
        //笑脸，不带发送按钮
        _contengScrollViewSmile = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 8, 320, 185)];
        _contengScrollViewSmile.delegate = self;
        _contengScrollViewSmile.backgroundColor = [UIColor clearColor];
        _contengScrollViewSmile.contentSize = CGSizeMake(320*1, 180);
        _contengScrollViewSmile.showsHorizontalScrollIndicator = NO;
        _contengScrollViewSmile.pagingEnabled = YES;
        _contengScrollViewSmile.tag=1000;
        [self addSubview:_contengScrollViewSmile];
        
        //笑脸，带发送按钮
        _contengScrollViewSmileSend = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 8, 320, 185)];
        _contengScrollViewSmileSend.delegate = self;
        _contengScrollViewSmileSend.backgroundColor = [UIColor clearColor];
        _contengScrollViewSmileSend.contentSize = CGSizeMake(320*1, 180);
        _contengScrollViewSmileSend.showsHorizontalScrollIndicator = NO;
        _contengScrollViewSmileSend.pagingEnabled = YES;
        _contengScrollViewSmileSend.tag=1001;
        [self addSubview:_contengScrollViewSmileSend];
        
        //加号键盘
        _contengScrollViewAdd = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 8,320 , 185)];//410
        _contengScrollViewAdd.delegate = self;
        _contengScrollViewAdd.backgroundColor = [UIColor clearColor];
        _contengScrollViewAdd.tag=1002;
        [self addSubview:_contengScrollViewAdd];
        
        //加号键盘的最后一行，不显示表情键盘的按钮，这里改成白条
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height-40, __MainScreen_Width, 40)];
        imageView.image = [UIImage imageNamed:@"bottom-bar.png"];
        [self addSubview:imageView];
        [imageView release];

        //分页控件
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 164, 320, 10)];
        _pageControl.backgroundColor = [UIColor clearColor];
        _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor darkGrayColor];
        _pageControl.currentPage = 0;
        [self addSubview:_pageControl];
        
        //表情键盘：表情种类按钮
        _contengScrollViewButton = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height-40, 320, 40)];
        _contengScrollViewButton.backgroundColor = [UIColor clearColor];
        _contengScrollViewButton.delegate = self;
        _contengScrollViewButton.tag = 1003;
        [self addSubview:_contengScrollViewButton];
    }
    return self;
}

- (void)dealloc
{
    self.delegate = nil;
    self.inputedStr = nil;
    
    [_contengScrollViewSmileSend release],_contengScrollViewSmileSend = nil;
    [_contengScrollViewAdd release],_contengScrollViewAdd = nil;
    [_contengScrollViewButton release],_contengScrollViewButton = nil;
    [_contengScrollViewCollect release],_contengScrollViewCollect = nil;
    [_contengScrollViewSmile release],_contengScrollViewSmile = nil;
    [_pageControl release],_pageControl = nil;
    
    [super dealloc];
}


-(void)layoutSubviews
{
    if(_keyboardType == KeyboardTypeEmo)//表情键盘
    {
        //添加表情种类按钮
        for(int i=0; i<2; i++)
        {
            UIButton * button1 =(UIButton *)[self viewWithTag:(10000 + i)];
            if (button1)
            {
                [button1 removeFromSuperview];
            }
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setBackgroundImage:[UIImage imageNamed:@"btn_em_selected.png"] forState:UIControlStateNormal];
            [button setTitle:i?NSLocalizedString(@"flagTitle", nil):NSLocalizedString(@"smileTitle", nil) forState:UIControlStateNormal];
            button.tag = 10000 + i;
            [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(typeChange:) forControlEvents:UIControlEventTouchUpInside];
            button.frame = CGRectMake(128*i, self.bounds.size.height-40, 128, 40);
            [self addSubview:button];
        }
        
        //添加删除按钮，发送按钮
        UIButton * cancelBtn1 =(UIButton *)[self viewWithTag:DeleteTag];
        if (cancelBtn1)
        {
            [cancelBtn1 removeFromSuperview];
        }
        UIButton *cancelBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        cancelBtn.frame = CGRectMake(256, self.bounds.size.height-40, 64, 40);
        cancelBtn.tag = DeleteTag;
        [cancelBtn setImage:[UIImage imageNamed:@"btn_em_delete.png"] forState:UIControlStateNormal];
        [cancelBtn setImage:[UIImage imageNamed:@"btn_em_delete_press.png"] forState:UIControlStateHighlighted];
        [cancelBtn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:cancelBtn];
        [cancelBtn release];
    }
    else if(_keyboardType == KeyboardTypeAdd)//加号键盘
    {
        for(UIView *view in self.subviews)
        {
            switch (view.tag)
            {
                case 10000:
                case 10001:
                case 10002:
                case DeleteTag:
                    view.hidden = YES;
                    break;
                    
                default:
                    break;
            }
        }
    }
}

#pragma mark - 设置键盘的类型，加号键盘|表情键盘
//设置“表情”大类中不同种类的表情（小表情、符号）
- (void)setType:(SmileViewType)type
{
    if(_type == type)
        return;
    _type = type;
    
    [self layoutSubviews];
    
    NSInteger page = 0;
    _pageControl.frame = CGRectMake(0, 164, 320, 10);
    if(type == SmileViewSmileType)
    {
        _contengScrollViewSmileSend.hidden = YES;
        _contengScrollViewAdd.hidden = YES;
        _contengScrollViewSmile.hidden = NO;
        _pageControl.numberOfPages = 2;
        _pageControl.currentPage = 0;
        if (!self.s1)
        {
            _pageControl.numberOfPages = 2;
            _contengScrollViewSmile.contentSize= CGSizeMake(320*_pageControl.numberOfPages, 140);
            NSArray  *smileArr = [NSArray arrayWithObjects:@"打怪兽",@"嘟嘟嘴",@"多变",@"饿了",@"高兴",@"歌王",@"好烦啊",@"加油",@"惊讶",@"可爱",@"可怜",@"麦霸",@"怒火",@"饶命",@"撒娇",@"睡觉",@"痛苦",@"羞羞脸",@"阴谋",@"眨眼",@"发大财",@"喷饭",@"炸飞了",@"凄凉",@"T台秀",@"读书",@"扭屁股",@"舞蹈",@"得意",@"运动",@"救命",@"滚远点",@"江南舞",@"走你", nil];
            
            for(int i=0; i<[smileArr count]; i++)
            {
                NSString *imageName = [NSString stringWithFormat:@"%@",[smileArr objectAtIndex:i]];
                NSString *path=[[NSBundle mainBundle]pathForResource:imageName ofType:@"gif"];
                SCGIFImageView *gif=[[SCGIFImageView alloc]initWithGIFFile:path];
                gif.userInteractionEnabled=YES;
                gif.frame=CGRectMake(320*page+10+52*(i%6), 50*(i/6-3*page), 35, 35);
                gif.backgroundColor=[UIColor blackColor];
                gif.tag=i;
                
                //加一个手势 让他触发方法
                UITapGestureRecognizer *tapGester=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnPress:)];
                [gif addGestureRecognizer:tapGester];
                [tapGester release];
                [_contengScrollViewSmile addSubview:gif];
                [gif release];
                if(i>16 && !page)
                    page = 1;
                if(i>34)
                {
                    page = 2;
                }
            }
            self.s1 = YES;
        }
    }
}

//设置表情键盘或者加号键盘
-(void)setKeyboardType:(KeyboardType)keyboardType
{
    if(_keyboardType == keyboardType)
        return;
    _keyboardType = keyboardType;
    
    [self layoutSubviews];
   
    _pageControl.frame = CGRectMake(0, 164, 320, 10);
    if (keyboardType == KeyboardTypeEmo)//表情键盘
    {
        //表情，默认显示上次选中的表情
        _type = [[UserDefaults valueForKey:@"smileType"] integerValue];
    }
    else if(keyboardType == KeyboardTypeAdd)//加号键盘
    {
        _contengScrollViewSmileSend.hidden = YES;
        _contengScrollViewAdd.hidden = NO;
        _contengScrollViewSmile.hidden = YES;
        _pageControl.numberOfPages = 2;
        _pageControl.frame = CGRectMake(0, 190, 320, 10);
        if (!self.s3)
        {
            self.contengScrollViewAdd.contentSize= CGSizeMake(320*1+90, 140);
            NSMutableArray *nameArr=[[NSMutableArray alloc]initWithObjects:@"拍照",@"相册",@"破冰语",@"名片",@"涂鸦",@"位置",@"魔法工具箱", @"burnAfterReading",nil];
            NSMutableArray * labelArr = [[NSMutableArray alloc]initWithObjects:NSLocalizedString(@"photo2Video", nil),NSLocalizedString(@"funLabel1", nil),NSLocalizedString(@"searchIce", nil),NSLocalizedString(@"visitingCard", nil),NSLocalizedString(@"funLabel3", nil),NSLocalizedString(@"locationBtn", nil),NSLocalizedString(@"magicTool", nil),NSLocalizedString(@"burnAfterReading", nil), nil];
            
            if(_delegate&&[_delegate respondsToSelector:@selector(addActionCard)])
            {
                NSString *name=[_delegate addActionCard];
                [nameArr addObject:name];
            }
            
            NSInteger page=0;
            for(int i=0;i<[nameArr count];i++)
            {
                NSString *imageName = [NSString stringWithFormat:@"%@.png",[nameArr objectAtIndex:i]];
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake(320*page+32+100*(i%4), 87*(i/4-2*page), 55, 55);
                [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
                [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
                [button addTarget:self action:@selector(actionToadd:) forControlEvents:UIControlEventTouchUpInside];
                button.tag = i;
                [self.contengScrollViewAdd addSubview:button];
                
                UILabel *label=[[UILabel alloc]init];
                label.frame=CGRectMake(320*page+15+100*(i%4)-10, 87*(i/4-2*page)+60, 85+20, 15);
                label.textAlignment=kTextAlignmentCenter;
                label.font = [UIFont systemFontOfSize:14];
                label.backgroundColor=[UIColor clearColor];
                NSString *str= [labelArr objectAtIndex:i];
                label.text=str;
                [self.contengScrollViewAdd addSubview:label];
                [label release];
                
                if(i>8 && !page)
                    page = 1;
            }
            [nameArr release];
            [labelArr release];
            
            self.s3 = YES;
        }
    }
}
#pragma mark - UIScrollView delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_type == SmileViewAddType)
    {
        _pageControl.currentPage = scrollView.contentOffset.x/60;
    }
    else
    {
        _pageControl.currentPage = scrollView.contentOffset.x/320.0;
    }
}

//表情键盘切换不同种类的表情(表情、符号、收藏)
- (void)typeChange:(UIButton *)button
{
    _pageControl.currentPage = (button.tag - 10000);
    _contengScrollViewSmile.contentOffset = CGPointMake(0, 0);
    self.type = button.tag - 10000;
}

#pragma mark - 表情键盘的代理处理
//点击表情后事件处理
-(void)btnPress:(UITapGestureRecognizer *)tap
{
    NSString *inputStr=nil;
    
    NSArray  *smileArr = [NSArray arrayWithObjects:@"打怪兽",@"嘟嘟嘴",@"多变",@"饿了",@"高兴",@"歌王",@"好烦啊",@"加油",@"惊讶",@"可爱",@"可怜",@"麦霸",@"怒火",@"饶命",@"撒娇",@"睡觉",@"痛苦",@"羞羞脸",@"阴谋",@"眨眼",@"发大财",@"喷饭",@"炸飞了",@"凄凉",@"T台秀",@"读书",@"扭屁股",@"舞蹈",@"得意",@"运动",@"救命",@"滚远点",@"江南舞",@"走你", nil];
    inputStr=[smileArr objectAtIndex:tap.view.tag];
    
    if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
        [_delegate banBu_SmileView:self didInputSmile:inputStr isDelete:NO type:1];
}

//点击符号后事件处理
- (void)btnPressed:(UIButton *)button
{
    if(button.tag == DeleteTag)
    {
        if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
            [_delegate banBu_SmileView:self didInputSmile:nil isDelete:YES type:0];
    }
    else
    {
        NSString *inputStr = nil;
        if(_type == SmileViewCharactersPaintedType)
        {    inputStr = button.titleLabel.text;
            [_inputedStr addObject:inputStr];
            if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
                [_delegate banBu_SmileView:self didInputSmile:inputStr isDelete:NO type:0];
        }
        else
        {
            NSString *smileStr = @"[吃包子]|[打怪兽]|[大跌眼镜]|[高歌一曲]|[歌舞王后]|[加油]|[泪流满面]|[路过]|[怒发冲冠]|[俏皮]|[求包养]|[求饶]|[睡觉]|[太帅了]|[头都大了]|[兴高采烈]|[羞羞脸]|[压力好大]|[阴谋得逞]|[睁大眼睛]|[装可爱]";
            inputStr = [NSString stringWithFormat:@"%@",[[smileStr componentsSeparatedByString:@"|"] objectAtIndex:button.tag]];
            [_inputedStr addObject:inputStr];
            if([_delegate respondsToSelector:@selector(banBu_SmileView:didInputSmile:isDelete:type:)])
                [_delegate banBu_SmileView:self didInputSmile:inputStr isDelete:NO type:1];
        }
    }
}

#pragma mark - 加号键盘的代理方法
//加号键盘处理
-(void)actionToadd:(UIButton *)bt
{
    if(_delegate&&[_delegate respondsToSelector:@selector(banBu_ActionView:didInputBrand:isAdd:)])
    {
        if(bt.tag==0)
        {
            [_delegate banBu_ActionView:self didInputBrand:bt isAdd:YES];
        }
        else
        {
            [_delegate banBu_ActionView:self didInputBrand:bt isAdd:NO];
        }
    }
}

@end
